package com.bubaiwantong.algorithms2.select;

import com.bubaiwantong.algorithms2.insert.SimpleInsertSort;

/**
 * All rights Reserved, Designed By www.tom.com
 *
 * @Author 徐望成
 * @Date 2023/11/7 11:33
 * @PackageName:com.bubaiwantong.algorithms2.select
 * @ClassName: SimpleSort
 * @Description: TODO
 * @Copyright: 2019 www.tomonline-inc.com Inc. All rights reserved.
 * 注意：本内容仅限于TOM集团内部传阅，禁止外泄以及用于其他的商业目
 * @Version 1.0
 */
public class SimpleSelectSort {
    public static void main(String[] args) {
        SimpleSelectSort simpleSelectSort = new SimpleSelectSort();
//        int[] arr = new int[]{9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
        int[] arr = new int[]{9, 8, 7, 6, 0, 5, 4, 3, 2, 1};
        simpleSelectSort.printArr(arr);
        simpleSelectSort.sort(arr);
        simpleSelectSort.printArr(arr);
    }

    /**
     * 简单插入排序
     * 选择一个合适的位置插入
     *
     * @param arr
     */
    public void sort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int minPos = i;
            for (int j = i; j < arr.length; j++) {
                if (arr[j] > arr[minPos]) {
                    minPos = j;
                }
                swap(arr,minPos,j);
            }
        }
    }

    public void printArr(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }

    public void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

}
